package com.smallA.service.Impl;

import com.smallA.constant.RequestConstant;
import com.smallA.mapper.GradeMapper;
import com.smallA.po.Exam;
import com.smallA.po.Grade;
import com.smallA.po.Test;
import com.smallA.service.GradeService;
import com.smallA.utils.DataUtils;
import com.smallA.utils.TypeConverterUtils;
import com.smallA.vo.Result;
import com.smallA.vo.ResultTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 考试表操作实现类
 *
 * @author 君未洋
 */
@Service("GradeService")
public class GradeServiceImpl implements GradeService {
    @Resource
    private GradeMapper gradeMapper;
    @Resource
    private DataUtils dataUtils;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<ResultTest> selectGradesById(Integer sid) {
        if (dataUtils.examineNumber(sid)) {
            return null;
        }
        List<Grade> grades = gradeMapper.selectGradesById(sid);
        List<ResultTest> resultTests = TypeConverterUtils.converterResultTest(grades);
        return resultTests;
    }

    @Override
    public Grade selectGradeById(Integer gid) {
        if (dataUtils.examineNumber(gid)) {
            return null;
        }
        return gradeMapper.selectGradeById(gid);
    }

    @Override
    public List<Grade> selectGradeByTestId(Integer tid) {
        if (dataUtils.examineNumber(tid)) {
            return null;
        }
        return gradeMapper.selectGradeByTestId(tid);
    }

    @Override
    public Result<Integer> getEndTime(Integer gid) {
        if (gid == null) {
            return new Result<>(400, RequestConstant.HTTP_400);
        }
        /*使用Redis,设定一个hash来保存这些数据*/
        HashOperations hash = redisTemplate.opsForHash();

        System.out.println(1);

        Grade grade = selectGradeById(gid);
        if (grade.getBeginTime() == null) {
            /*如果这里的开始时间为空代表有问题,直接返回坏参数*/
            return new Result<>(400, RequestConstant.HTTP_400);
        }
        System.out.println("22");
        Object useTime = hash.get("UseTime", gid.toString());
        System.out.println("33");
        //通过这个方法来获取存放到hash里面的时间,如果获取不到就说明考试刚刚开始
        if (useTime == null) {
            System.out.println(2);
            /*没有获取到,创建一个新的hash用来保存,顺便计算下现在开始考试多久了*/
            int useT = (int) ((grade.getBeginTime().getTime() - System.currentTimeMillis()) / 1000);
            hash.put("UseTime", gid, useT);
            return new Result<>(grade.getTest().getTestTime() - useT);
        }

        System.out.println(3);

        /*没有问题就可以计算剩余时间并给剩下的自增下*/
        int anInt = Integer.parseInt(useTime.toString());
        //现在就更新下使用时间
        hash.increment("UseTime", gid, 1);
        System.out.println(4);
        return new Result<>(grade.getTest().getTestTime() - anInt - 1);
    }

    @Override
    public Result<Exam> beginTest(Integer tid, Integer uid) {
        return null;
    }

    @Override
    public boolean insertGrade(Integer sid, Integer tid) {
        //做一个小小的查询看看是否重复报名了
        if (dataUtils.examineNumber(sid, tid)) {
            return false;
        }
        if (gradeMapper.selectGradeExam(sid, tid) != null) {
            return false;
        }
        return gradeMapper.insertGrade(sid, tid) > 0;
    }


    @Override
    public boolean BeginGrade(Integer gid) {
        if (dataUtils.examineNumber(gid)) {
            return false;
        }
        Grade grade = gradeMapper.selectGradeById(gid);
        Test test = grade.getTest();
        if (test.getBeginDate().getTime() > System.currentTimeMillis()) {
            //如果设定的考试开始时间大于当前时间代表还未能开始考试,直接返回false;
            return false;
            //代表开始失败
        }
        if (test.getId() <= 0) {
            //代表这个测验有问题,终止考试
            return false;
        }
        if (grade.getId() <= 0 || grade.getStudentId() <= 0) {
            //这个是代表这个成绩表的数据有问题
            return false;
        }
        /*AutoSubmit autoSubmit = new AutoSubmit(test.getTestTime(), "1990609801@qq.com");
        autoSubmit.setMsg("亲爱的用户\n您参加的考试" + test.getContext() + "已结束,请在5min内提交试卷,否则将取消考试成绩");
        new Thread(autoSubmit).start();*/
        //开启并执行线程
        //直接开始考试
        return gradeMapper.updateGradeTime(grade.getId(), new Date()) > 0;
        //其他未判断到的情况就直接返回false
    }

    @Override
    public boolean EndGrade(Integer gid) {
        if (dataUtils.examineNumber(gid)) {
            return false;
        }
        Grade grade = gradeMapper.selectGradeById(gid);
        long useTime = (System.currentTimeMillis() - grade.getBeginTime().getTime()) / (1000 * 60);
        return gradeMapper.updateGradeEnd(gid, (int) useTime) > 0;
    }


}
